#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "icar_log.h"
#include "icar_pkg.h"
#include "icar_end_msg.h"
#include "icar_flat_msg.h"
#include "icar_utils.h"
#include <stdint.h>

extern int idaemon;
extern uint16_t icar_msg_seq;

int main(int argc, char **argv)
{
	icar_daemon(LOG_DEBUG, "Yes");
	char *num = "18689831418";
	int port = 60000;
	char buf[1024] = { 0 };
	struct sockaddr_in addr;
	int fd;

	char authcode[200] = { 0 };
	memset(authcode, '1', 199);
	int n = strlen(authcode);
	printf("n: %d\n", n);
	static int cnt = 0;

recycle:
	if (cnt)
		usleep(100000);
	fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd < 0)
	{
		perror("socket");
		return -1;
	}

	bzero(&addr, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	addr.sin_addr.s_addr = inet_addr("127.0.0.1");

	if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
		perror("connect");
		close(fd);
		return -1;
	}

	if (!cnt)
		cnt = 1;

	while (1) {
		flat_diagnose(fd, "HQ", 2);
		/* 
		if ((n = read(fd, buf, sizeof(buf))) == 0)
		{
			close(fd);
			fd = -1;
			goto recycle;

		}
		*/

		sleep(2);
	}

	close(fd);

	return 0;
}
